#include <iostream>
#include <cmath>
#include "StarAndMap.h""
using namespace std;

double distance(int x1, int y1, int x2, int y2)
{
    int a = x1 - x2;
    int b = y1 - y2;
    return sqrt(a * a + b * b);
}

Map::Map(int a,int b)//星系数，文明数
{
    this->n_stars = a;
    s = new star[a];
    this->n_civ = b;
    c = new civ[b];
    for (int i = 0; i < b; i += 1)
    {
        c[i].own = new star * [a];
        c[i].observed = new star * [a];
        /*	for (int i1 = 0; i1 < a; i += 1)
        {
            c[i].own[i] = NULL;
            c[i].observed[i] = NULL;
        }*/
    }
}

void Map::mapgen(int a)
{
    srand(time(NULL));
    for (int i = 0; i < a; i += 1)
    {
        if (i == 0)
        {
            s[i].x = abs(rand() % length);
            s[i].y = abs(rand() % height);
            s[i].n = i;
            s[i].res = rand() % 5 + 1;
            s[i].poplim = rand() % 90 + 10;
        }
        else
        {
            s[i].n = i;
            s[i].res = rand() % 5 + 1;
            s[i].poplim = rand() % 90 + 10;
            int token = 0;
            while (token == 0)
            {
                int temp_x = abs(rand() % length);
                int temp_y = abs(rand() % height);
                int token1 = 0;
                for (int i1 = 0; i1 < i && token1 == 0; i1 += 1)
                {
                    if (distance(temp_x, temp_y, s[i1].x, s[i1].y) < 20)
                    {
                        token1 = 1;
                    }
                }
                if (token1 == 1)
                {
                    token1 = 0;
                    continue;
                }
                if (token1 == 0)
                {
                    s[i].x = temp_x;
                    s[i].y = temp_y;
                    token = 1;
                }
            }
            token = 0;
        }
    }
}

void Map::civdis(int a, int b)//星系数，文明数
{
    srand(time(NULL));
    int* assigned = new int[a];
    for (int i = 0; i < b; )
    {
        int temp = rand() % a;
        int token;
        token = 0;
        for (int i1 = 0; i1 < i&&token==0; i1 += 1)
        {
            if (assigned[i1] == temp)
            {
                token = 1;
            }
        }
        if (token == 0)
        {
            c[i].num = i;
            c[i].own[0] = &s[temp];
            assigned[i] = temp;
            i += 1;
        }
        token = 0;
    }
    delete[] assigned;
}

civ::civ(int t)
{
    own = new star * [t];
    observed = new star * [t];
    for (int i = 0; i < t; i += 1)
    {
        own[i] = NULL;
        observed[i] = NULL;
    }
}

star::star():button_icon(":/new/images/images/HugeStar.png")
{
}
void star::change()
{
    if(selected==0)
    {
        QIcon *newIcon=new QIcon (":/new/images/images/icon_1.png");
        this->setIcon(*newIcon);
        delete newIcon;
        selected=1;
    }
    else
    {
        QIcon *newIcon=new QIcon (":/new/images/images/HugeStar.png");
        this->setIcon(*newIcon);
        delete newIcon;
        selected=0;
    }
}
